package com.summer.mall.controller;

import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.User;
import com.summer.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService create(@RequestParam Integer shippingId){
        User user = (User)httpSession.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return orderService.createOrder(user.getId(), shippingId);
    }


    @RequestMapping(value = "/cancel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseService cancel(@RequestParam Long orderNo){
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return  ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return orderService.cancel(user.getId(),orderNo);
    }

    @RequestMapping("/get_order_cart_product")
    @ResponseBody
    public ResponseService getOrderCartProduct() {
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return orderService.getOrderCartProduct(user.getId());
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ResponseService detail( @RequestParam Long orderNo) {
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return orderService.getOrderDetail(user.getId(), orderNo);
    }

    /**
    * @Author : complone
    * @Desrcption: 获取该用户的所有订单记录
    * @return 返回一个封装的OrderVo对象
    * @Date  8:45
    **/
    @RequestMapping("/list")
    @ResponseBody
    public ResponseService list( @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return orderService.getOrderList(user.getId(), pageNum, pageSize);
    }

    /**
     * 支付
     */
    @RequestMapping("/pay")
    @ResponseBody
    public ResponseService pay(@RequestParam Long orderNo) {
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return orderService.pay( orderNo,user.getId());
    }





}
